package com.smart.stock.module.spider.jobSpiderWeb.board;

import com.smart.stock.common.util.DateUtils;
import com.smart.stock.common.util.spring.EntityUtil;
import com.smart.stock.common.util.spring.SpringContextUtil;
import com.smart.stock.module.GlobalConstant;
import com.smart.stock.module.maindata.entity.BoardIndustry;
import com.smart.stock.module.spider.AbstractHtmlSpider;
import com.smart.stock.module.spider.entity.WebPage;
import com.smart.stock.module.spider.util.RequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 同花顺行业板块
 *
 **/
@Slf4j
@Component
public class BoardIndustrySpider extends AbstractHtmlSpider<BoardIndustry> {
    String entityName = EntityUtil.getEntityNameByCode(BoardIndustry.class, null);

    public MongoTemplate getMongoTemp() {
        return SpringContextUtil.getBean(MongoTemplate.class);
    }

    @Override
    public WebPage crawlPage() {
        this.useAgent = SpringContextUtil.getProperties("spider.useagent");
        this.pageUrl = GlobalConstant.URL_THS_STOCK_TODAY;
        this.referer = GlobalConstant.URL_THS_INDUSTRY_REFERRER;
        this.method = Connection.Method.GET;
        Map<String, String> cookie = RequestUtil.string2token(GlobalConstant.URL_THS_INDUSTRY_COOKIESTR);
        this.cookie = cookie;
        return super.crawlPage();
    }

    @Override
    public void parsePage(WebPage webPage) throws Exception {
        if (null == webPage) {
            return;
        }
        String regex = ".*\\：|\\s*";
        Document doc = webPage.getDocument();
//        序号	板块	涨跌幅(%)	总成交量（万手）	总成交额（亿元）	净流入（亿元）	上涨家数	下跌家数	均价	领涨股	最新价	涨跌幅(%)

        Elements trs = doc.select("tbody").select("tr");
        getMongoTemp().remove(BoardIndustry.class);
        for (int i = 0; i < trs.size(); i++) {
            Element tr = trs.get(i);
            BoardIndustry industry = new BoardIndustry();
            industry.setTradeDate(Integer.valueOf(DateUtils.current_date_yyyymmdd()));
            industry.setName(tr.select("td").get(1).text());
            industry.setPctchg(Double.valueOf(tr.select("td").get(2).text()));
            industry.setVol(Double.valueOf(tr.select("td").get(3).text()));
            industry.setAmount(Double.valueOf(tr.select("td").get(4).text()));
            industry.setInAmount(Double.valueOf(tr.select("td").get(5).text()));
            industry.setUps(Integer.valueOf(tr.select("td").get(6).text()));
            industry.setDowns(Integer.valueOf(tr.select("td").get(7).text()));
            getMongoTemp().insert(industry);
        }




    }


}
